package com.assetmanage.dao.impl;

import com.assetmanage.dao.UserDao;
import com.assetmanage.model.User;
import com.assetmanage.utils.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao {
    @Override
    public User login(String username, String password) {
        String sql = "SELECT * FROM users WHERE u_name = ? AND password = ?";
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            conn = DBUtil.getConnection();
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, username);
            stmt.setString(2, password);
            rs = stmt.executeQuery();
            if (rs.next()) {
                User user = new User();
                user.setUId(rs.getInt("u_id"));
                user.setUName(rs.getString("u_name"));
                user.setPassword(rs.getString("password"));
                user.setDepId(rs.getString("dep_id"));
                user.setAdmin(rs.getString("admin"));
                return user;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, stmt, rs);
        }
        return null;
    }

    @Override
    public List<User> getAllUsers() {
        List<User> users = new ArrayList<>();
        String sql = "SELECT * FROM users";
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            conn = DBUtil.getConnection();
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);
            while (rs.next()) {
                User user = new User();
                user.setUId(rs.getInt("u_id"));
                user.setUName(rs.getString("u_name"));
                user.setPassword(rs.getString("password"));
                user.setDepId(rs.getString("dep_id"));
                user.setAdmin(rs.getString("admin"));
                users.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, stmt, rs);
        }
        return users;
    }

    @Override
    public List<User> getUsersByDepartment(String depId) {
        List<User> users = new ArrayList<>();
        String sql = "SELECT * FROM users WHERE dep_id = ?";
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            conn = DBUtil.getConnection();
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, depId);
            rs = stmt.executeQuery();
            while (rs.next()) {
                User user = new User();
                user.setUId(rs.getInt("u_id"));
                user.setUName(rs.getString("u_name"));
                user.setPassword(rs.getString("password"));
                user.setDepId(rs.getString("dep_id"));
                user.setAdmin(rs.getString("admin"));
                users.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, stmt, rs);
        }
        return users;
    }

    @Override
    public boolean addUser(User user) {
        String sql = "INSERT INTO users (u_id, u_name, password, dep_id, admin) VALUES (?, ?, ?, ?, ?)";
        Connection conn = null;
        PreparedStatement stmt = null;
        try {
            conn = DBUtil.getConnection();
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, user.getUId());
            stmt.setString(2, user.getUName());
            stmt.setString(3, user.getPassword());
            stmt.setString(4, user.getDepId());
            stmt.setString(5, user.getAdmin());
            return stmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            DBUtil.close(conn, stmt, null);
        }
    }

    @Override
    public boolean updateUser(User user) {
        String sql = "UPDATE users SET u_name = ?, password = ?, dep_id = ?, admin = ? WHERE u_id = ?";
        Connection conn = null;
        PreparedStatement stmt = null;
        try {
            conn = DBUtil.getConnection();
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, user.getUName());
            stmt.setString(2, user.getPassword());
            stmt.setString(3, user.getDepId());
            stmt.setString(4, user.getAdmin());
            stmt.setInt(5, user.getUId());
            return stmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            DBUtil.close(conn, stmt, null);
        }
    }

    @Override
    public boolean deleteUser(int uId) {
        String sql = "DELETE FROM users WHERE u_id = ?";
        Connection conn = null;
        PreparedStatement stmt = null;
        try {
            conn = DBUtil.getConnection();
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, uId);
            return stmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            DBUtil.close(conn, stmt, null);
        }
    }

    @Override
    public User getUserById(int uId) {
        String sql = "SELECT * FROM users WHERE u_id = ?";
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            conn = DBUtil.getConnection();
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, uId);
            rs = stmt.executeQuery();
            if (rs.next()) {
                User user = new User();
                user.setUId(rs.getInt("u_id"));
                user.setUName(rs.getString("u_name"));
                user.setPassword(rs.getString("password"));
                user.setDepId(rs.getString("dep_id"));
                user.setAdmin(rs.getString("admin"));
                return user;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, stmt, rs);
        }
        return null;
    }
} 